package com.hontech.bv.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import com.hontech.bv.R
import com.hontech.bv.conn.Device
import com.hontech.bv.conn.Proto
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.window.showProg

private class PingSubView(view: View, titleId: Int, tvId: Int, btnId: Int, val destId: Int) {

    private val tvTitle: TextView
    private val tvState: TextView
    private val btnPing: Button

    init {
        tvTitle = view.findViewById(titleId)
        tvState = view.findViewById(tvId)
        btnPing = view.findViewById(btnId)

        btnPing.onClick(::onPing)
    }

    suspend fun ping() {
        tvState.text = "未知"
        try {
            Device.ping(destId)
            tvState.text = "正常"
        } catch (e: Exception) {
            tvState.text = "异常"
            throw e
        }
    }

    private suspend fun onPing() {
        val dialog = showProg(Proto.nameId(destId), "Ping")
        try {
            ping()
            dialog.success("Ping成功")
        } catch (e: Exception) {
            dialog.error(e)
        }
    }

    fun hide() {
        tvTitle.visibility = View.GONE
        tvState.visibility = View.GONE
        btnPing.visibility = View.GONE
    }

    fun show() {
        tvTitle.visibility = View.VISIBLE
        tvState.visibility = View.VISIBLE
        btnPing.visibility = View.VISIBLE
    }
}

class SinglePingView(ctx: Context, attrs: AttributeSet) : FrameLayout(ctx, attrs) {

    private val btnHide: Button

    private val subPipeLine: PingSubView
    private val subPick: PingSubView
    private val subArm: PingSubView
    private val subPearl: PingSubView
    private val subPowder: PingSubView
    private val subIce: PingSubView
    private val subHeat: PingSubView
    private val btnAll: Button

    private var showFlag = false

    init {
        val view = LayoutInflater.from(ctx).inflate(R.layout.view_single_ping, this)
        btnHide = view.findViewById(R.id.id_single_ping_btn_hide)
        btnAll = view.findViewById(R.id.id_single_ping_btn_all)

        subPipeLine = PingSubView(
            view,
            R.id.id_single_ping_tv_pipe_line_title,
            R.id.id_single_ping_tv_pipe_line,
            R.id.id_single_ping_btn_pipe_line,
            Proto.ID_MASTER
        )

        subPick = PingSubView(
            view,
            R.id.id_single_ping_tv_pick_title,
            R.id.id_single_ping_tv_pick,
            R.id.id_single_ping_btn_pick,
            Proto.ID_PICK
        )

        subArm = PingSubView(
            view,
            R.id.id_single_ping_tv_arm_title,
            R.id.id_single_ping_tv_arm,
            R.id.id_single_ping_btn_arm,
            Proto.ID_ARM
        )

        subPearl = PingSubView(
            view,
            R.id.id_single_ping_tv_pearl_title,
            R.id.id_single_ping_tv_pearl,
            R.id.id_single_ping_btn_pearl,
            Proto.ID_PEARL
        )

        subPowder = PingSubView(
            view,
            R.id.id_single_ping_tv_powder_title,
            R.id.id_single_ping_tv_powder,
            R.id.id_single_ping_btn_powder,
            Proto.ID_POWDER
        )

        subIce = PingSubView(
            view,
            R.id.id_single_ping_tv_ice_title,
            R.id.id_single_ping_tv_ice,
            R.id.id_single_ping_btn_ice,
            Proto.ID_ICE
        )

        subHeat = PingSubView(
            view,
            R.id.id_single_ping_tv_heat_title,
            R.id.id_single_ping_tv_heat,
            R.id.id_single_ping_btn_heat,
            Proto.ID_HEAT
        )

        hide()

        btnHide.setOnClickListener {
            if (showFlag) {
                hide()
            } else {
                show()
            }
        }
        btnAll.onClick(::pingAll)
    }

    private suspend fun pingAll() {
        val dialog = showProg("Ping所有", "流水线控制板")
        var err: Exception? = null

        try {
            subPipeLine.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("取物门控制板")
        try {
            subPick.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("机械臂控制板")
        try {
            subArm.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("制冰机控制板")
        try {
            subIce.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("珍珠架控制板")
        try {
            subPearl.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("粉料架控制板")
        try {
            subPowder.ping()
        } catch (e: Exception) {
            err = e
        }

        dialog.setMsg("加热控制板")
        try {
            subHeat.ping()
        } catch (e: Exception) {
            err = e
        }

        if (err != null) {
            dialog.error(err)
            return
        }
        dialog.success("成功")

    }

    private fun show() {
        subHeat.show()
        subPick.show()
        subArm.show()
        subIce.show()
        subPipeLine.show()
        subPearl.show()
        subPowder.show()
        btnAll.visibility = View.VISIBLE
        showFlag = true
        btnHide.text = "隐藏"
    }

    private fun hide() {
        btnHide.text = "显示"
        subHeat.hide()
        subPick.hide()
        subArm.hide()
        subIce.hide()
        subPipeLine.hide()
        subPearl.hide()
        subPowder.hide()
        btnAll.visibility = View.GONE
        showFlag = false
    }
}























